package com.itic.appbase.applications.sys.user.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.relation.persistence.dao.UserRoleDao;
import com.itic.appbase.applications.sys.relation.persistence.model.UserRole;
import com.itic.appbase.applications.sys.relation.persistence.model.UserRoleExample;
import com.itic.appbase.applications.sys.relation.persistence.model.UserRoleExample.Criteria;
import com.itic.appbase.applications.sys.user.persistence.dao.SysUserDao;
import com.itic.appbase.applications.sys.user.persistence.model.SysUser;
import com.itic.appbase.applications.sys.user.persistence.model.SysUserExample;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.MD5Helper;
import com.itic.appbase.framework.utils.PropertyGetter;
import com.itic.appbase.framework.utils.StringHelper;

@Service
@Transactional
public class SysUserService
        extends CrudService<SysUserDao, SysUser, SysUserExample> {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private UserRoleDao userRoleDao;

    /**
     * 添加用户信息
     * 
     * @param sysUser 用户信息
     * @param roleList 角色列表
     * @return
     */
    public String addUser(SysUser sysUser, List<UserRole> roleList) {
        String result = super.add(sysUser);
        if (SUCCESS.equals(result)) {
            for (UserRole userRole : roleList) {
                this.initDefalutFields(userRole); // 初始化6个必填字段
                int code = userRoleDao.insert(userRole);
                if (code < 1) {
                    result = FAILED;
                }
            }
        }
        return result;
    }

    /**
     * 删除用户信息
     * 
     * @param sysUser 用户信息
     * @param example 用户example
     * @param userIdList 用户id集合
     * @return
     */
    public String deleteUser(SysUser sysUser, SysUserExample example, List<String> userIdList) {
        String result = FAILED;
        int code = sysUserDao.updateByExampleSelective(sysUser, example); // 逻辑删除用户
        if (code > 0) {
            code = userRoleDao.deleteRoleByUserIds(userIdList); // 根据用户id物理删除用户角色关系
            if (code < 0) {
                result = FAILED;
            }
            result = SUCCESS;
        }
        return result;
    }

    /**
     * 用户角色分配
     * 
     * @param roleIds userRoleList
     * @return
     */
    public String roleAssign(String userId, List<UserRole> userRoleList) {
        String result = FAILED;
        // 先删除最初分配的角色
        int code = userRoleDao.deleteRoleByUserId(userId); // 物理删除成功标志为code>=0
       
        if (code >= 0) {
            for (UserRole userRole : userRoleList) {
                this.initDefalutFields(userRole); // 初始化6个必填字段
                code = userRoleDao.insert(userRole);
                if (code < 1) {
                    result = FAILED;
                }
            }
            result = SUCCESS;
        }

        return result;
    }

    /**
     * 重置密码
     * 
     * @param userList 用户list
     */
    public String resetPwd(String resetPwd, List<SysUser> userList) {
        String result = SUCCESS;
        for (SysUser sysUser : userList) {
            sysUser.setPassword(MD5Helper.md5DoubleSalt(resetPwd, sysUser.getUsername()));
            sysUser.setUpdateTime(new Date());
            int code = sysUserDao.updateByPrimaryKeySelective(sysUser);
            if (code < 1) {
                result = FAILED;
            }
        }
        return result;
    }

}